const fs = require('fs');
const path = require('path');

const svgPath = 'D:\\gitee项目\\wujie 无界vue3\\command-center-v3-common\\src\\assets_0.1.0\\img';
const initFolderPath = 'D:\\gitee项目\\wujie 无界vue3\\command-center-v3-common\\src';

const svgJson = {}
// 已生成过的(避免重复生成)
const generated = {}

function readFilesRecursively(folderPath) {
  const files = fs.readdirSync(folderPath);
  files.forEach(file => {
    const filePath = path.join(folderPath, file);
    const stats = fs.statSync(filePath);
    if (stats.isDirectory()) {
      readFilesRecursively(filePath);
    } else if (stats.isFile()) {
      const list = filePath.split('.')
      const suffix = list[list.length - 1]
      if (suffix === 'vue') {
        // 在这里处理文件
        getFileData(filePath)
      } else if (suffix === 'ts') {
        getFileData(filePath)
      }
    }
  });
}

function getFileData (path) {
  fs.readFile(path, 'utf8', (err, data) => {
    if (err) {
      console.error(err);
      return;
    }
    getSvgContent(data)
  });
}

// 获取 vue 的 svg内容
function getSvgContent (initContent) {
  const lines = initContent.split(/\r?\n/);
  lines.forEach((line, index) => {
    if (line.includes('console.') || line.includes('//') || line.includes('/*') || line.includes('* ') || line.includes('<!--')) {
      return
    }
    line.replace(/"([\s\S]*?)"/g, (_,svgName) => {
      if (!svgName.indexOf('#icon-')) {
        generateNewSsvg(svgName.replace('#icon-', ''))
      } else {
        generateNewSsvg(svgName)
      }
    })
    line.replace(/'([\s\S]*?)'/g, (_,svgName) => {
      if (!svgName.indexOf('#icon-')) {
        generateNewSsvg(svgName.replace('#icon-', ''))
      } else {
        generateNewSsvg(svgName)
      }
    })
  });
}

// 生成新增的svg
function generateNewSsvg (svgName) {
  const path = svgJson[`${svgName}`]
  if (path) {
    if (!generated[`${svgName}`]) {
      generated[`${svgName}`] = svgName
      fs.readFile(path, 'utf8', (err, data) => {
        if (err) {
          console.error(err);
          return;
        }
        const list = path.replace(`${svgPath}\\`, '').split('\\')
        const listLength =  list.length - 1
        let mkdirNmae = ''
        let svgName = ''
        list.forEach((item,index) => {
          if (index === listLength) {
            svgName = item
          } else {
            mkdirNmae += item + '/'
          }
        })
        fs.mkdir(`./img/${mkdirNmae}`, {recursive: true}, (err) => {
          if (err) return callback(err);
          fs.writeFile(`./img/${mkdirNmae}${svgName}`, data, 'utf8', (err) => {
            if (err) {
              console.error(err);
              return;
            }
          });
        });
      });
    }
  }

}

function getSvgJosn(folderPath) {
    const files = fs.readdirSync(folderPath);
    files.forEach( file => {
      const filePath = path.join(folderPath, file);
      const stats = fs.statSync(filePath);
      if (stats.isDirectory()) {
         getSvgJosn(filePath);
      } else if (stats.isFile()) {
        const key = filePath.replace(`${svgPath}\\`, '').replace(/\\/g,'-').replace(/\.svg/g,'')
        svgJson[`${key}`] = filePath
      }
    });
}

getSvgJosn(svgPath);
readFilesRecursively(initFolderPath);


